package com.zt.student.controller;

import com.zt.common.Do.ClassWithTeachDO;
import com.zt.common.common.ApiException;
import com.zt.common.common.CodeMsg;
import com.zt.common.pojo.ActiveClass;
import com.zt.common.pojo.ClassSelection;
import com.zt.common.pojo.common.JsonResult;
import com.zt.common.pojo.common.SemesterStatus;
import com.zt.common.utils.ClassUtil;
import com.zt.student.dto.ClassScoreDTO;
import com.zt.student.service.ActiveClassService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * description:
 * author:zt
 * date:2021-12-26
 */
@RestController
@CrossOrigin
@RequestMapping("activeClass")
@Api("课堂模块")
public class ActiveClassController {

    @Autowired
    private ActiveClassService activeClassService;

    @GetMapping("getSemester/{n}")
    @ApiOperation("获取未来n个学期及其状态")
    public JsonResult<ArrayList<SemesterStatus>> getSemester(@PathVariable int n){

        return JsonResult.success(ClassUtil.getSemesterStatus(n));
    }

    @GetMapping("getClass/{semester}")
    @ApiOperation("获取学生所有可选课堂")
    public JsonResult<List<ClassWithTeachDO>> getAllActiveClass(@PathVariable String semester, HttpServletRequest request){
        List<ClassWithTeachDO> activeClasses = activeClassService.getAllActiveClass(semester,request);
        return JsonResult.success(activeClasses);
    }

    @GetMapping("getOptionalClass/{semester}")
    @ApiOperation("获取学生除去时间冲突的课程外的可选课堂")
    public JsonResult<List<ClassWithTeachDO>> getOptionalActiveClass(@PathVariable String semester, HttpServletRequest request){
        List<ClassWithTeachDO> activeClasses = activeClassService.getOptionalActiveClass(semester,request);
        return JsonResult.success(activeClasses);
    }

    @GetMapping("getSelectedClass/all/{semester}")
    @ApiOperation("获取学生已选全部课堂")
    public JsonResult<List<ClassWithTeachDO>> getAllSelectedClass(@PathVariable String semester, HttpServletRequest request){
        List<ClassWithTeachDO> activeClasses = activeClassService.getAllSelectedClass(semester, request);
        return JsonResult.success(activeClasses);
    }

    @GetMapping("getSelectedClass/elective/{semester}")
    @ApiOperation("获取学生已选选修通选课堂")
    public JsonResult<List<ClassWithTeachDO>> getSelectedElectiveClass(@PathVariable String semester,HttpServletRequest request){
        List<ClassWithTeachDO> activeClasses = activeClassService.getSelectedElectiveClass(semester, request);
        return JsonResult.success(activeClasses);
    }

    @GetMapping("getSelectedClass/compulsory/{semester}")
    @ApiOperation("获取学生已选必修课堂")
    public JsonResult<List<ClassWithTeachDO>> getSelectedCompulsoryClass(@PathVariable String semester,HttpServletRequest request){
        List<ClassWithTeachDO> activeClasses = activeClassService.getSelectedCompulsoryClass(semester, request);
        return JsonResult.success(activeClasses);
    }

    @GetMapping("getSchedule/{semester}")
    @ApiOperation("获取学生课程表")
    public JsonResult<List<List<ClassWithTeachDO>>> getSchedule(@PathVariable String semester, HttpServletRequest request){
        List<List<ClassWithTeachDO>> schedule = activeClassService.getSchedule(semester, request);
        return JsonResult.success(schedule);
    }

    @GetMapping("getResources/{classId}")
    @ApiOperation("根据班级id获取该班级所有资源名字")
    public JsonResult<String[]> getResources(@PathVariable int classId){
        try {
            String[] resources = ClassUtil.getClassResources(classId);
            return JsonResult.success(resources);
        }catch (NullPointerException e){
            throw new ApiException(CodeMsg.CLASS_NOT_EXISTED);
        }
    }

    @GetMapping("getScore/{semester}")
    @ApiOperation("根据学期获取学生课程名字与分数")
    public JsonResult<List<ClassScoreDTO>> getClassScore(@PathVariable String semester, HttpServletRequest request){
        return JsonResult.success(activeClassService.getClassScore(request,semester));
    }

}
